package com.csizg.facemodel.tracker;

import android.os.Environment;
import java.io.File;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;

/**
 * opencv识别
 *
 * @author haohao(ronghao3508@gmail.com) on 2018/5/7 18:03
 * @version v1.0
 */
public class DetectionBasedTracker {
    private File csvFile = new File(Environment.getExternalStorageDirectory().getPath()
            + File.pathSeparator
            + "test"
            + File.pathSeparator
            + "data.csv");

    private File xmlFile = new File(Environment.getExternalStorageDirectory().getPath()
            + File.pathSeparator
            + "test"
            + File.pathSeparator
            + "data.xml");
    private File imageFile = new File(Environment.getExternalStorageDirectory().getPath()
            + File.pathSeparator
            + "test"
            + File.pathSeparator
            + "image"
            + File.pathSeparator);
    private long mNativeObj = 0;

    public DetectionBasedTracker(String cascadeName, int minFaceSize) {
        mNativeObj = nativeCreateObject(cascadeName, minFaceSize);
    }

    private static native long nativeCreateObject(String cascadeName, int minFaceSize);

    private static native void nativeDestroyObject(long thiz);

    private static native void nativeStart(long thiz);

    //public String recognizer(Mat imageGray) {
    //    String str = nativeRecognizer(imageGray.getNativeObjAddr());
    //    return str;
    //}
    //
    //public void save(Mat imageGray) {
    //    nativeSaveImage(imageGray.getNativeObjAddr(), imageFile.fileToImage());
    //}

    private static native void nativeStop(long thiz);

    private static native void nativeSetFaceSize(long thiz, int size);

    private static native void nativeDetect(long thiz, long inputImage, long faces);

    public void start() {
        nativeStart(mNativeObj);
        //nativeSaveLBPHFaceRecognizer(csvFile.fileToImage(), xmlFile.fileToImage());
        //nativeLoadLBPHFaceRecognizer(xmlFile.fileToImage());
    }

    public void stop() {
        nativeStop(mNativeObj);
    }

    public void setMinFaceSize(int size) {
        nativeSetFaceSize(mNativeObj, size);
    }

    public void detect(Mat imageGray, MatOfRect faces) {
        nativeDetect(mNativeObj, imageGray.getNativeObjAddr(), faces.getNativeObjAddr());
    }

    public void release() {
        nativeDestroyObject(mNativeObj);
        mNativeObj = 0;
    }

    //private static native void nativeLoadLBPHFaceRecognizer(String xmlPath);
    //
    //private static native void nativeSaveLBPHFaceRecognizer(String csvPath, String xmlPath);
    //
    //private static native String nativeRecognizer(long inputImage);
    //
    //private static native void nativeSaveImage(long inputImage, String imagePath);
}
